package ahp.client.layouts;

import java.util.ArrayList;

import ahp.client.slider.Slider;
import ahp.client.slider.SliderOption;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

public class KategorieLayout extends Composite {
	public final FlowPanel porownanie = new FlowPanel();
	public final Button btnNewButton = new Button("Akceptuj");
	
	final double[][] macierzPorownan;
	ArrayList<String> nazwyKategorii;
	final ArrayList<Slider> all_slider = new ArrayList<Slider>();

	public boolean flaga = false;
	
	
	public KategorieLayout(final double[][] _macierzPorownan,
			ArrayList<String> _nazwyKategorii) {

		macierzPorownan = _macierzPorownan;
		nazwyKategorii = _nazwyKategorii;
		initWidget(porownanie);
		porownanie.setStyleName("kategory-list");

		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				update();
				flaga = true;

			}
		});

		
		porownanie.addStyleName("porownanie");

		int index_slidera = 0;
		for (int i = 0; i < macierzPorownan.length; i++) {
			for (int j = i + 1; j < macierzPorownan.length; j++) {
				FlowPanel panel = new FlowPanel();
				panel.addStyleName("kategory");
				Label l1 = new Label(nazwyKategorii.get(i));
				Label l2 = new Label(nazwyKategorii.get(j));
				l1.setStyleName("left");
				l2.setStyleName("right");
				final int x = i;
				final int y = j;

				JSONObject options = Slider.getOptions(-4, 4, new int[] { 0 });
				options.put(SliderOption.STEP.toString(), new JSONNumber(1));
				Slider slider = new Slider("slider" + index_slidera, options);
				slider.addStyleName("slider");
				slider.i = i;
				slider.j = j;
				all_slider.add(slider);

				index_slidera++;
				panel.add(l1);
				panel.add(slider);
				panel.add(l2);

				porownanie.add(panel);
			}
		}
		porownanie.add(btnNewButton);
	}

	public void update() {

		for (Slider s : all_slider) {
			int i = s.i;
			int j = s.j;
			int value = s.getValue();
			double real_value = 1;
			if (value == -4 || value == 4) {
				real_value = 9;
			} else if (value == -3 || value == 3) {
				real_value = 7;
			} else if (value == -2 || value == 2) {
				real_value = 5;
			} else if (value == -1 || value == 1) {
				real_value = 3;
			} else {
				real_value = 1;
			}

			if (value > 0) {
				macierzPorownan[j][i] = real_value;
				macierzPorownan[i][j] = 1 / real_value;

			} else if (value < 0) {
				macierzPorownan[i][j] = real_value;
				macierzPorownan[j][i] = 1 / real_value;

			} else {
				macierzPorownan[i][j] = 1;
				macierzPorownan[j][i] = 1;
			}
			// System.out.println("i =" + s.i + " j= " + s.j + " value=" +
			// s.getValue());
		}

	}

}
